(function($) {
	
	var pages = {},
		home = 'index.html',
		init = true,
		target = window.location.href.replace(/\\/g,'/').replace(/.*\//,'').split('#'),
		self = target[0],
		wrap;
	
	target = target[target.length -1];
	
	$(window).hashchange(function() {
		var hash = location.hash.replace(/^#/,'');
		page = pages[hash === '' ? home : hash];
		if (page) {
			if (page.title) document.title = page.title;
			page.fn();
		}
	});
	
	$.fn.flynav = function(options) {

		var settings = $.extend({
				selector: 'article, .article',
				selectedClass: 'fn-selected',
				positions: {},
				autoLeft: 0,
				autoTop: 0
				// onLoad()
				// onDeselect()
				// obSelect()
			},options),
			content = $(settings.selector),
			parent = content.parent(),
			links = (this).find('a'),
			idx = 0,
			load, prev;
		
		if (init) {
			parent.append($('<div class="flynav-wrap">'));
			wrap = $('.flynav-wrap');
			init = false;
		}

		if (settings.autoLeft == 0 && settings.autoTop == 0) settings.autoLeft = "idx*100+'%'";
		if (settings.home) home = settings.home;
		if (target === '') target = settings.home || home;	
		if (self === '') self = settings.home || home;	
		
		var prepare = function() {
			
			var a = $(this),
				href = a.attr('href'),
				wpos = {},
				page, name, postfix;

			if (pages[href]) return;

			if (!settings.positions[href]) {
				var l,t;
				eval('l = (' + settings.autoLeft + '); t = (' + settings.autoTop + ');');
				settings.positions[href] = { left: l, top: t };
			}

			var pos = settings.positions[href];

			for (name in pos) {
				if (name === 'scale') wpos[name] = 1/pos[name];
				else if (name === 'easing') wpos[name] = pos[name];
				else {
					postfix = null;
					if (typeof pos[name] === 'string') postfix = pos[name].match(/[^0-9]+$/);
					wpos[name] = -parseFloat(pos[name]) + (postfix ? postfix[0] : 0);
				}
			}

			if (wpos.scale) {
				wpos.left *= wpos.scale;
				wpos.top *= wpos.scale;
			}

			if (wpos.rotate) {
				var n = Math.sqrt(wpos.left * wpos.left + wpos.top * wpos.top),
					ang = Math.atan2(wpos.top,wpos.left) + wpos.rotate * (Math.PI / 180);
				wpos.left = n * Math.cos(ang);
				wpos.top = n * Math.sin(ang);
			}

			var pageData = pages[href] = {
				fn: function() { 
					if (settings.onBeforeHide) settings.onBeforeHide.apply(prev);
					if (settings.onBeforeShow) settings.onBeforeShow.apply(page);
					if (page) wrap.stop().transition($.extend({},wpos),function() {
						if (settings.onHide) settings.onHide.apply(prev);
						if (settings.onShow) settings.onShow.apply(page);
						prev = page;
					});
					links.removeClass(settings.selectedClass);
					a.addClass(settings.selectedClass);
					return false;
				}
			}

			$('a[href="' + href + '"]').attr('href','#' + href);

			if (href === self) {
				pageData.title = $('head title').text();
				load = page = content.wrap('<div class="flynav-page">').parent().css(pos);
				wrap.append(load);
			} else {
				$.get(href,function(data) {
					var title = data.match(/<head[\s\S]*?<title.*?>([\s\S]*?)<\/title.*?/im);
					if (title) pageData.title = title[1];
					var content = data.match(/<body[[\s\S]*?>([\s\S]*)<\/body>/im);
					if (content) {
						content = $('<div>' + content[1] + '</div>').find(settings.selector);
						if (content) {
							wrap.append(content);	
							page = content.wrap('<div class="flynav-page">').parent().css(pos);
							if (settings.onLoad) settings.onLoad.apply(page[0]);
							if (href === target) {
								prev = page;
								if (settings.onShow) settings.onShow.apply(page);
							}
						}
					}
				},'text');
			}

			if (href === target) {
				wrap.css(wpos);
				a.addClass(settings.selectedClass);
			}

			idx++;

		};
		
		links.each(prepare);

		if (load) {
			if (settings.onLoad) settings.onLoad.apply(load[0]);
			if (home === target) {
				prev = load;
				if (settings.onShow) settings.onShow.apply(load);
			}
		}
			
		return this;

	};

})(jQuery);
